package com.Al.pattern.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author lizhigang Date: 2019/4/18 9:54
 * @description:
 */
public class ContainerSingleton {
	private ContainerSingleton(){};

	public static Map<String,Object> ioc = new ConcurrentHashMap<String, java.lang.Object>();

	private Object object;

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}
	//注册式容器单例方案，同时注册多个单例对象
	public static Object getInstance(String className){
		synchronized (ioc) {
			if (!ioc.containsKey(className)) {
				Object obj = null;
				try {
					obj = Class.forName(className).newInstance();
					ioc.put(className, obj);

				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				}
			}
			return ioc.get(className);
		}
	}
}
